package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class UserHelper {

    /**
     * 根据用户id获取用户信息
     * @param list 用户id列表
     * @param userMap 用户容器
     * @param userList 存放用户的结果列表
     * @return 用户结果列表名称的拼接
     */
    public static String getUserNameByUserIds(List<String> list, Map<String, UserEntity> userMap, List<UserEntity> userList){
        String result = "";
        for(Object userId: list) {
            UserEntity userEntity = userMap.get(userId);
            if(Objects.nonNull(userEntity)){
                userList.add(userEntity);
                result = new StringBuilder().append(result + "、" + userEntity.getName()).toString();
            }
        }
        return result;
    }

    /**
     * 根据attr获取成员多选的信息
     * @param attr 字段对应的标识
     * @param objectJsonObject 数据
     * @param userMap 用户容器
     * @return "list":[{"id":"", "name":""}]
     */
    public static List<UserEntity> getUserByKey(String attr, JSONObject objectJsonObject, Map<String, UserEntity> userMap){
        List<UserEntity> userList = new ArrayList<>();
        if(StringUtil.isNotEmpty(objectJsonObject.getString(attr))){
            JSONArray jsonArray = objectJsonObject.getJSONArray(attr);
            if (CollectionUtils.isEmpty(jsonArray)) {
                return userList;
            }
            List<String> list = JSON.parseArray(JSON.toJSONString(jsonArray), String.class);
            if (!(jsonArray.get(0) instanceof String)) {
                // 这里增加个jsonObject的判断，有部分位置会前置拼装，所以这里兼容下
                list = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    if (Objects.isNull(jsonObject)) {
                        continue;
                    }
                    if (StringUtil.isNotEmpty(jsonObject.getString("id"))) {
                        list.add(jsonObject.getString("id"));
                    }
                }
            }
            if(!list.isEmpty()){
                getUserNameByUserIds(list, userMap, userList);
            }
        }
        return userList;
    }

    /**
     * 根据部门id获取部门信息
     * @param list 部门id列表
     * @param departmentMap 部门容器
     * @param deptList 存放部门id对应的结果列表
     * @return 部门id对应的结果的名称拼接字符串
     */
    public static String getDepartmentNameByDeptIds(List<String> list, Map<String, DepartmentEntity> departmentMap, List<DepartmentEntity> deptList){
        String result = "";
        for(Object deptId: list) {
            DepartmentEntity departmentEntity = departmentMap.get(deptId);
            if(Objects.nonNull(departmentEntity)){
                deptList.add(departmentMap.get(deptId));
                result = new StringBuilder().append(result + "、" + departmentMap.get(deptId).getName()).toString();
            }
        }
        return result;
    }

    /**
     * 根据attr获取部门多选的信息
     * @param attr 字段标识
     * @param objectJsonObject 单行数据
     * @param departmentMap 部门容器
     * @return 返回[{"id":"", "name":""}]
     */
    public static List<DepartmentEntity> getDepartmentByKey(String attr, JSONObject objectJsonObject, Map<String, DepartmentEntity> departmentMap){
        List<DepartmentEntity> deptList = new ArrayList<>();
        Object value = objectJsonObject.get(attr);
        if(Objects.nonNull(value) && StringUtil.isNotEmpty(objectJsonObject.getString(attr))){
            JSONArray jsonArray = objectJsonObject.getJSONArray(attr);
            List<String> list = JSONArray.parseArray(JSON.toJSONString(jsonArray), String.class);
            if(!list.isEmpty()){
                getDepartmentNameByDeptIds(list, departmentMap, deptList);
            }
        }
        return deptList;
    }
}
